/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multithreadserver;
import java.io.*;
import java.net.*;
/**
 *
 * @author Brady Itkin
 */
public class Protocol {
//    private static final int WAITING = 0;
//    private static final int ASKING = 1;
//    private static final int WRITING = 2;
//    private static final int ANOTHER = 3;
    public static final int WELCOME = 0;
    public static final int SAYSOMETHING = 1;
    public static final int BYE = 2;
    
    public int state = WELCOME;
    PrintWriter FileOutputStream = null;

    public void Writeln(String Line) throws IOException{
        int i;
        Socket s;
        
        FileOutputStream =
            new PrintWriter(new FileWriter("messages.txt",true));
        FileOutputStream.println(Line);
        FileOutputStream.close();
        for (i=0;i<Main.socklist.size();i++){
            s=(Socket) Main.socklist.get(i);
            FileOutputStream = new PrintWriter(s.getOutputStream(), true);
            FileOutputStream.println(Line);
            
        }
    }
    
    String ProcessInput(String input) throws IOException {
        //TODO: make this return more than just an output string
        //TODO: implement commands
        //TODO: make this design more efficiente
        String output = null;
        if (state == WELCOME) {
            output="WELCOME TO RAIDERCHAT...SAY SOMETHING!\n\n\n>";
            state=SAYSOMETHING;
        }
        else if (state==SAYSOMETHING) {
            output="\n";
            Writeln(input);
            state=SAYSOMETHING;
        }
//        if (state == WAITING) {
//            output = "Hello, would you like to leave a message? Y or N";
//            state = ASKING;
//        }
//       // else if (state == ASKING) {
//            if (input.equalsIgnoreCase("Y")) {
//                output = "OK, please leave your one line message: ";
//                state = WRITING;
//            }
//            else {
//                output = "Bye!";
//                state = WAITING;
//            }
//        }
//        else if (state == WRITING) {
//            Writeln(input);
//            output = "DONE Y FOR ANOTHER ONE, N TO QUIT";
//            state = ASKING;
//        }
        return output;
    }
}